package com.partners.component.aop;

import com.alibaba.fastjson.JSONObject;
import com.partners.configure.TokenConfig;
import com.partners.constant.ErrorMessageEnum;
import com.partners.dao.dto.UserTestDTO;
import com.partners.model.BusinessException;
import com.partners.model.Member;
import com.partners.model.TokenCodeModel;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

/**
 * @author liangyongle
 * @date 2019-01-15
 */
@Aspect
@Component
@Slf4j
public class TestAop {

    @Resource
    private TokenConfig tokenConfig;

    @Pointcut("execution(* com.partners.controller.*.token*(..))")
    public void testValid(){}

    @Around("testValid()")
    public Object doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();

        StringBuilder bodyStrBuilder = new StringBuilder();
        ServletInputStream is = request.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = reader.readLine()) != null) {
            bodyStrBuilder.append(line);
        }
        String bodyStr = bodyStrBuilder.toString();

        JSONObject json = JSONObject.parseObject(bodyStr);
        log.info("json:{}",json);
        if (json != null) {
            String token = json.getString("token");
            if(token !=null){
                Object[] args = proceedingJoinPoint.getArgs();
                if (args == null || args.length == 0) {
                    return proceedingJoinPoint.proceed();
                }
                UserTestDTO userTestDTO = new UserTestDTO();
                //获取内容
                Member member = new Member();
                member.setToken(token);
                TokenCodeModel tokens = tokenConfig.createAndSaveToken(member);
                userTestDTO.setId(tokens.getId());
                userTestDTO.setName(tokens.getName());
                userTestDTO.setOpenId(tokens.getOpenId());
                userTestDTO.setToken(token);
                MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
                Method currentMethod = methodSignature.getMethod();
                Parameter[] parameters = currentMethod.getParameters();
                for (int i=0; i< args.length; i++) {
                    Parameter parameter = parameters[i];
                    if (UserTestDTO.class.equals(parameter.getType())) {
                        args[i] = userTestDTO;
                        break;
                    }
                }

                return proceedingJoinPoint.proceed(args);
            }else{
                System.out.println(2);
            }
            return proceedingJoinPoint.proceed();
            //throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        //throw BusinessException.of(ErrorMsgEnum.E4);

    }
}
